<template>
  <view class="page">
    <!-- 导航栏 -->
    <!-- 内容区域 -->
    <scroll-view class="content-wrapper" scroll-y>
      <view class="detail-container">
        <!-- 标题 -->
        <view class="news-title">{{ newsDetail.title }}</view>

        <!-- 时间 -->
        <view class="news-time">
          <text class="time-text">{{ newsDetail.time }}</text>
        </view>

        <!-- 正文内容 -->
        <view class="news-content">
          <uv-parse :content="newsDetail.content" :selectable="true"></uv-parse>
        </view>
      </view>
    </scroll-view>
  </view>
</template>

<script>
export default {
  data() {
    const sys = uni.getSystemInfoSync()
    const statusBarPx = sys.statusBarHeight || 0

    return {
      statusBarPx,
      newsDetail: {
        id: '',
        title: '',
        content: '',
        time: '',
        timestamp: 0
      }
    }
  },

  onLoad() {
    // 从缓存获取新闻详情
    try {
      const newsData = uni.getStorageSync('current_news_detail')
      if (newsData) {
        this.newsDetail = JSON.parse(newsData)
        console.log('从缓存加载新闻数据:', this.newsDetail.id)
      } else {
        console.warn('缓存中未找到新闻数据')
        this.showErrorAndGoBack()
      }
    } catch (error) {
      console.error('读取新闻数据失败:', error)
      this.showErrorAndGoBack()
    }  
  },

  onUnload() {
    // 页面卸载时清除缓存，避免占用存储空间
    try {
      uni.removeStorageSync('current_news_detail')
      console.log('已清除新闻详情缓存')
    } catch (error) {
      console.error('清除缓存失败:', error)
    }
  },

  methods: {
    /**
     * 显示错误提示并返回上一页
     */
    showErrorAndGoBack() {
      uni.showToast({
        title: '数据加载失败',
        icon: 'none'
      })
      setTimeout(() => {
        uni.navigateBack()
      }, 1500)
    },

    /**
     * 返回上一页
     */
    goBack() {
      uni.navigateBack()
    }
  }
}
</script>

<style lang="scss" scoped>
.page {  
  width: 100%;
  background: #f7f8fa;
}
/* 内容区域 */
.content-wrapper {
  width: 100%;
  box-sizing: border-box;
}

.detail-container {
  padding: 32rpx 24rpx;
  background: #ffffff;
  margin: 0;
  min-height: calc(100vh - 88rpx);
}

/* 标题 */
.news-title {
  font-size: 36rpx;
  font-weight: 700;
  color: #1a1a1a;
  line-height: 1.6;
  margin-bottom: 24rpx;
}

/* 时间 */
.news-time {
  display: flex;
  align-items: center;
  margin-bottom: 32rpx;
  padding-bottom: 24rpx;
  border-bottom: 1rpx solid #e4e7ed;
}

.time-text {
  font-size: 26rpx;
  color: #909399;
}

/* 正文内容 */
.news-content {
  font-size: 28rpx;
  color: #606266;
  line-height: 1.8;
  word-break: break-word;
  
  /* uv-parse 组件样式微调 */
  :deep(.uv-parse-p) {
    margin: 0 0 20rpx 0;
    font-size: 28rpx;
    color: #606266;
    line-height: 1.8;
  }
  
  :deep(.uv-parse-img) {
    max-width: 100%;
    border-radius: 8rpx;
    margin: 20rpx 0;
  }
  
  :deep(a) {
    color: #00d4aa;
  }
  
  :deep(strong), :deep(b) {
    font-weight: 600;
    color: #303133;
  }
}
</style>

